package com.xigua.数组;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/3/1913:39
 */
public class _2200_找出数组中的所有K近邻下标 {


    public static void main(String[] args) {
//        findKDistantIndices(new int[]{734,228,636,204,552,732,686,461,973,874,90,537,939,986,855,387,
//                344,939,552,389,116,93,545,805,572,306,157,899,276,479,337,219,936,416,457,612,795,221,
//                51,363,667,112,686,21,416,264,942,2,127,47,151,277,603,842,586,630,508,147,866,434,973,
//                216,656,413,504,360,990,228,22,368,660,945,99,685,28,725,673,545,918,733,158,254,207,742,
//                705,432,771,578,549,228,766,998,782,757,561,444,426,625,706,946},939,34);

//        findKDistantIndices(new int[]{3,4,9,1,3,9,5},9,1);
//        findKDistantIndices(new int[]{1,1000,1,1000}, 1,1);
        findKDistantIndices(new int[]{2,2,2,2,2},2,2);



    }

    public static List<Integer> findKDistantIndices(int[] nums, int key, int k) {
        List<Integer> ans = new ArrayList<>();
        int i = 0;
        int end;
        int start = 0;
        while (i < nums.length){
            if (nums[i] == key){
                end = i+k+1;
                for (int j = Math.max(i-k,start); j < Math.min(end,nums.length) ; j++) {
                    if (nums[j] == key && j != i){
                        end += j-i;
                    }
                    ans.add(j);
                }
                i = end;
                start = end;
            }else {
                i++;
            }
        }
        return ans;

    }

}
